package menu.lt.pers.menu.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import menu.lt.pers.menu.Bean.UserBean;

public class UserDao {
    private DBHelper helper;
    private SQLiteDatabase db;

    public UserDao(Context context) {
        helper = new DBHelper(context,Dbdata.dbname,null,Dbdata.version);
        db = helper.getReadableDatabase();
    }
    //数据库插入操作
    public void insert(UserBean user){
        String sql="insert into "+Dbdata.userTable+" ("+Dbdata.userColumn[4]+","+Dbdata.userColumn[5]
                + ")values(?,?)";
        db.execSQL(sql,
                new String[]{user.getPhone(),user.getPwd()});
    }
    public Cursor query(){
        Cursor cursor=db.query(Dbdata.userTable,null,null,null,null,null,null);
        return cursor;
    }

    public Cursor findByPhone(UserBean userBean){
        String phone = userBean.getPhone();
        String selection = Dbdata.userColumn[4]+" = ?";
        String[] selectionArgs = new String[]{userBean.getPhone()};
        Cursor cursor=db.query(Dbdata.userTable,null,selection,selectionArgs,null,null,null);
        return cursor;
    }

    public void delete(int _id){
        String sql = "delete from " + Dbdata.userTable + " where _id = "+ _id;
        db.execSQL(sql);
    }

    public  void update(UserBean userBean){
        String sql="UPDATE "+Dbdata.userTable+" SET "+
                Dbdata.userColumn[1]+" = ?, "+
                Dbdata.userColumn[2]+" = ?, "+
                Dbdata.userColumn[3]+" = ?, "+
                Dbdata.userColumn[4]+" = ?, "+
                Dbdata.userColumn[5]+" = ? "+
                " WHERE "+Dbdata.userColumn[0]+" = ? ;";
        db.execSQL(sql,
                new String[]{userBean.getUsername(),String.valueOf(userBean.getIsdelete()),
                        userBean.getUserhead(),userBean.getPhone(),userBean.getPwd(),String.valueOf(userBean.getId())});
    }


}
